package com.yunkaweilai.android.view.dialog.guadan;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.github.lazylibrary.util.StringUtils;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.interfaces.SimpleInterface;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.MoneyTextWatcher;
import com.yunkaweilai.android.utils.Utils;
import com.yunkaweilai.android.view.MyToast;

import java.text.DecimalFormat;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Administrator on 2017/10/23.
 */

public class GuadanReturnGoodsDialog extends Dialog {
    private Context context;
    private String type;
    private String gid;
    private String goods_id;
    private float nums;
    private String names;
    private SimpleInterface simpleInterface;
    private MaterialDialog mProgress;
    private TextView idTvRight;
    private ImageView idImgClose;
    private TextView idTvName;
    private TextView idTvReturnNum;
    private EditText idEdtReturnNum;

    public GuadanReturnGoodsDialog(Context context, int themeResId, SimpleInterface simpleInterface, String gid, String goods_id, float nums, String names) {
        super(context, themeResId);
        this.simpleInterface = simpleInterface;
        this.context = context;
        this.gid = gid;
        this.goods_id = goods_id;
        this.nums = nums;
        this.names = names;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_guadan_return_goods);
        initView();
        idTvName.setText(names);
        DecimalFormat df = new DecimalFormat("0.00");
        idTvReturnNum.setText(df.format(nums)+"");
        initClick();

    }

    private void initClick() {
        idImgClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        idTvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (StringUtils.isEmpty(idEdtReturnNum.getText().toString())) {
                    MyToast.showShortToast(context,"还未输入数量");
                    return;
                }
                if (Float.parseFloat(idTvReturnNum.getText().toString()) < Float.parseFloat(idEdtReturnNum.getText().toString())){
                    MyToast.showShortToast(context,"可退货数量不足");
                    return;
                }
                mProgress = new MaterialDialog.Builder(context)
                        //.title(R.string.progress_dialog)
                        .content(R.string.progress_content)
                        .progress(true, 0)
                        .show();
                initHttp();
            }
        });

    }

    private void initView() {
        idTvRight = ((TextView) findViewById(R.id.id_tv_right));
        idImgClose = ((ImageView) findViewById(R.id.id_img_close));
        idTvName = ((TextView) findViewById(R.id.id_tv_name));
        idTvReturnNum = ((TextView) findViewById(R.id.id_tv_return_num));
        idEdtReturnNum = ((EditText) findViewById(R.id.id_edt_return_num));
        idEdtReturnNum.addTextChangedListener(new MoneyTextWatcher(idEdtReturnNum));
    }


    private void initHttp() {
        HttpRequest.postUrl(Api.GUADAN_GOOD_RETURN)
                .addParams("o_gid",gid)
                .addParams("order_goods_id",goods_id)
                .addParams("refund_num",idEdtReturnNum.getText().toString())
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(context, json);
                        if (checkCode){
                            if (mProgress != null){
                                mProgress.dismiss();
                            }
                            MyToast.showShortToast(context,"退货成功");
                            simpleInterface.nothing();
                            dismiss();
                        }
                    }

                    @Override
                    public void onError(Exception e) {

                    }
                });

    }
}
